#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include "jsoncpp/json/json.h"
//#include "jsoncpp/json/writer.h"
// #include "jsoncpp/json/assertions.h"
// #include "jsoncpp/json/config.h"
// #include "jsoncpp/json/forwards.h"
// #include "jsoncpp/json/json_features.h"
// #include "jsoncpp/json/reader.h"
// #include "jsoncpp/json/value.h"
// #include "jsoncpp/json/version.h"
using namespace std;


void unserialize(const string &str)
{
    Json::CharReaderBuilder crb;
    Json::CharReader* cr = crb.newCharReader();
    Json::Value root;
    string err;
    bool ret = cr->parse(str.c_str(),str.c_str()+str.size(),&root,&err);
    if(ret == false)
    {
        cout << "json failed: " << err << endl;
        exit(-2);
    }

    cout << "name:" << root["name"].asCString() << endl;
    cout << "age:" << root["age"].asInt() << endl;
    int sz = root["score"].size();
    for(int i = 0; i < sz; i++)
    {
        cout << "score:" << root["score"][i].asFloat() << endl;
    }
    delete cr;
}


int main()
{
    Json::Value root;
    root["name"] = "xiaoming";
    root["age"] = 18;
    root["score"] .append(98);
    root["score"] .append(87.5);
    root["score"] .append(78.5);

    Json::StreamWriterBuilder swb;

    Json::StreamWriter* sw = swb.newStreamWriter();

    stringstream ss;
    int ret = sw->write(root,&ss);
    if(ret != 0)
    {
        cout << "Json fail" << endl;
        return -1;
    }

    cout << ss.str() << endl;

    unserialize(ss.str());
    delete sw;
    return 0;
}


